package keye.document_manage;

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.mybatis.spring.MyBatisSystemException;

import keye.document_manage.gui.ConnectView;
import keye.document_manage.main.MainFrame;
import keye.document_manage.util.IrregularFormSample;
import keye.document_manage.util.MyConstants;

public class StartMain {

	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConnectView connectView = new ConnectView();
					connectView.setIconImage(connectView.getToolkit().getImage(
							MyConstants.PICTURE_PATH + "mainIoc.png"));
					connectView.getConnectBtn().addActionListener(e -> {
						connectView.setVisible(false);
						start(connectView);
					});
					connectView.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void start(JFrame oldFrame) {
		IrregularFormSample sample = new IrregularFormSample(
				MyConstants.PICTURE_PATH + "startIoc.png");
		Image startIoc = sample.getToolkit().getImage(
				MyConstants.PICTURE_PATH + "startIoc.png");
		sample.setIconImage(startIoc);
		sample.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		sample.setVisible(true);
		EventQueue.invokeLater(() -> {
			MainFrame frame = null;
			try {
				frame = new MainFrame();
				Image mainIoc = sample.getToolkit().getImage(
						MyConstants.PICTURE_PATH + "mainIoc.png");
				frame.setIconImage(mainIoc);
				frame.setTitle("发布系统");
				frame.setLocationRelativeTo(null);
				UIManager.setLookAndFeel(UIManager
						.getSystemLookAndFeelClassName());
				SwingUtilities.updateComponentTreeUI(frame);
				frame.setBounds(100, 100, 800, 600);
				frame.setExtendedState(Frame.MAXIMIZED_BOTH);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
				System.out.println("启动成功！");
				oldFrame.dispose();
				sample.dispose();
			} catch (MyBatisSystemException e) {
				if (frame != null) {
					frame.dispose();
				}
				if (sample != null) {
					sample.dispose();
				}
				JOptionPane.showMessageDialog(null, "连接超时！", "数据库连接错误",
						JOptionPane.ERROR_MESSAGE);
				oldFrame.setVisible(true);
			} catch (Exception e) {
				System.out.println("----------startMain---------");
				e.printStackTrace();
			}
		});
	}
}
